<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加角色 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>

      <!-- 角色列表区域 -->
      <el-table border stripe :data="rolesListData">
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <!-- 第一层 -->
            <el-row
              v-for="item1 in scope.row.children"
              :key="item1.id"
              class="roles-level-1"
            >
              <el-col :span="4">
                <el-tag
                  closable
                  @close="removeRolesById(scope.row, item1.id)"
                  >{{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <!-- 第二层 -->
                <el-row
                  v-for="item2 in item1.children"
                  :key="item2.id"
                  class="roles-level-2"
                >
                  <el-col :span="8"
                    ><el-tag
                      type="success"
                      closable
                      @close="removeRolesById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    ><i class="el-icon-caret-right"></i
                  ></el-col>
                  <el-col :span="16">
                    <!-- 第三层 -->
                    <el-row
                      :span="24"
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      class="roles-level-3"
                    >
                      <el-col>
                        <el-tag
                          type="warning"
                          closable
                          @close="removeRolesById(scope.row, item3.id)"
                          >{{ item3.authName }}</el-tag
                        >
                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引行 -->
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="400px">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button type="primary" icon="el-icon-edit" size="mini"
              >编辑</el-button
            >
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini"
              >删除</el-button
            >
            <!-- 分配权限 -->
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="allotRightsBtn(scope.row)"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="rolesAllotRightsDialogVisible"
      width="50%"
      @close="rolesAllotRightsDialogClose"
    >
      <!-- 树形 -->
      <el-tree
        :data="treeRightsData"
        node-key="id"
        :props="treeRightsProps"
        default-expand-all
        show-checkbox
        :default-checked-keys="defaultCheckedKeysId"
        ref="treeAllKeysRef"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rolesAllotRightsDialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表数据
      rolesListData: [],
      // 控制分配权限对话框显示与隐藏
      rolesAllotRightsDialogVisible: false,
      // 树状权限数据
      treeRightsData: [],
      // 树状权限配置项
      treeRightsProps: {
        // 指定节点标签为节点对象的某个属性值
        label: 'authName',
        children: 'children'
      },
      // 默认勾选的节点
      defaultCheckedKeysId: [],
      // 角色ID
      roleId: ''
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 获取角色列表数据
    async getRolesList() {
      const { data: res } = await this.$http.get('roles')
      // 将获取的角色列表数据保存为私有数据
      this.rolesListData = res.data
      //   console.log(this.rolesListData)
    },
    // 删除Tag标签触发回调函数
    async removeRolesById(rolesId, rightsId) {
      const confirmReceive = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(error => error)
      //   console.log(confirmReceive)
      if (confirmReceive !== 'confirm') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete(
        `roles/${rolesId.id}/rights/${rightsId}`
      )
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      rolesId.children = res.data
    },
    // 分配权限按钮
    async allotRightsBtn(roles) {
      // 获取tree树状权限
      const { data: res } = await this.$http.get('rights/tree')
      // 树状权限数据
      this.treeRightsData = res.data
      console.log(this.rolesListData)
      this.roleId = roles.id
      console.log(this.roleId)
      this.getRightsTreeId(roles, this.defaultCheckedKeysId)
      // 显示分配权限对话框
      this.rolesAllotRightsDialogVisible = true
    },
    // 获取tree最后一层的ID
    getRightsTreeId(roles, arr) {
      if (!roles.children) {
        return arr.push(roles.id)
      }
      roles.children.forEach(item => {
        return this.getRightsTreeId(item, arr)
      })
    },
    // 关闭分配权限对话框回调函数
    rolesAllotRightsDialogClose() {
      this.defaultCheckedKeysId = []
    },
    // 分配权限
    async allotRights() {
      // 半选中ID
      const keys1 = this.$refs.treeAllKeysRef.getHalfCheckedKeys()
      // 全选中ID
      const keys2 = this.$refs.treeAllKeysRef.getCheckedKeys()
      // 数组转换成以，分割的字符串
      const arrStr = [...keys1, ...keys2].join(',')
      // 发送请求角色授权
      const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, { rids: arrStr })
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 提示更新成功
      this.$message.success(res.meta.msg)
      // 关闭分配权限对话框
      this.rolesAllotRightsDialogVisible = false
      // 重新发送请求获取角色列表
      this.getRolesList()
    }
  }
}
</script>

<style lang="less" scoped>
.roles-level-1 {
  display: flex;
  border-top: 1px solid #eee;
  align-items: center;
  padding: 0 10px;
  &:last-child {
    border-bottom: 1px solid #eee;
  }
  .roles-level-2 {
    display: flex;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid #eee;
    &:last-child {
      border-bottom: none;
    }
    .roles-level-3 {
      display: inline-block;
      margin: 10px;
    }
  }
  .el-icon-caret-right {
    padding-left: 5px;
  }
}
</style>
